package com.gdpu.project.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;
import org.apache.tools.ant.util.DateUtils;

public class DateConverter extends StrutsTypeConverter {
	private  static String DATA_FOMART="yyyy-MM-dd";
	
	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		Date date=null;
		if(values!=null&&values.length>0){
			String dateString=values[0];
			SimpleDateFormat format=new SimpleDateFormat(DATA_FOMART);
			try {
				date=format.parse(dateString);
			} catch (ParseException e) {
//				e.printStackTrace();
				return null;
			}
		}
		return date;
	}

	@Override
	public String convertToString(Map context, Object o) {
		Date date=(Date)o;
		String dateString=DateUtils.format(date, DATA_FOMART);
		return dateString;
	}

}
